package com.situ.b2c.register.dao;

import com.situ.b2c.pojo.User;
import com.situ.b2c.util.JDBCUtil;

public class UserDao {
	private static final String SQL_INSERT = "INSERT INTO TB_USER(USER_NAME,USER_CODE,USER_PASS,CREATE_BY,CREATE_DATE)VALUES(?,?,?,?,?)";
	private static final String SQL_SELECT_ONE_BY_CODE_AND_PASS = "SELECT * FROM TB_USER WHERE ACTIVE_FLAG = 1 AND USER_CODE=? AND USER_PASS = ?";
	private static final String SQL_SELECT_ONE_BY_CODE = "SELECT * FROM TB_USER WHERE ACTIVE_FLAG = 1 AND USER_CODE=?";	
	private static final String SQL_SELECT_ONE_BY_ID = "SELECT * FROM TB_USER WHERE ACTIVE_FLAG = 1 AND ROW_ID=?";	
	private static final String SQL_UPDATE = "UPDATE TB_USER SET USER_NAME=?, USER_CODE=?,USER_PASS=?,USER_AVATAR=?,IS_LOCK=?,UPDATE_BY=?,UPDATE_DATE=?,LOGIN_COUNT=?,LOGIN_IP=?,LOGIN_DATE=? WHERE ROW_ID=? ";
	
	
	public int insert(User user) {
		
		return JDBCUtil.executeUpdate(SQL_INSERT, user.getUserName(),user.getUserCode(),user.getUserPass(),user.getCreateBy(),user.getCreateDate());
		
	}
	
	
	public User selectOneByCodeAndPass(String userCode,String userPass) {
		
		return JDBCUtil.executeQuery4One(User.class, SQL_SELECT_ONE_BY_CODE_AND_PASS, userCode,userPass);
		
	}
	
	public User selectOneByCode(String userCode) {
		return JDBCUtil.executeQuery4One(User.class, SQL_SELECT_ONE_BY_CODE, userCode);
	}
	
	public User selectOneById(Long rowId) {
		return JDBCUtil.executeQuery4One(User.class, SQL_SELECT_ONE_BY_ID, rowId);
		
	}
	
	public int update(User user) {
		return JDBCUtil.executeUpdate(SQL_UPDATE,user.getUserName(), user.getUserCode(),user.getUserPass(),user.getUserAvatar(),user.getIsLock(),user.getUpdateBy(),user.getUpdateDate(),user.getLoginCount(),user.getLoginIp(),user.getLoginDate(),user.getRowId());
		
	}
	
}
